package com.cetc.nsfx.controller.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cetc.nsfx.service.sys.WordLibraryService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/25 0025.
 */
@Controller
@RequestMapping("/wordLibrary")
public class WordLibraryController {

    @Autowired
    private WordLibraryService wordLibraryService;

    @RequestMapping("/config")
    public String main(HttpServletRequest req){
        req.setAttribute("menu_sys",true);
        return "sys/wordLibrary/config";
    }

    @RequestMapping("/doSearch")
    public void doSearch(HttpServletRequest req,HttpServletResponse response){
        String word = req.getParameter("word");
        System.out.println("word="+word);
        String[] arr = wordLibraryService.suggestWord(word);
        this.sendJSON(response,"{\"a\":\"b\"}");

    }

    @RequestMapping("/addWord")
    public void addWord(HttpServletRequest req,HttpServletResponse response){
        String word = req.getParameter("word");
        System.out.println("word="+word);
        boolean exisit = wordLibraryService.findWord(word);
        Map<String,Object> result = new HashMap<String,Object>();
        if(exisit){
            result.put("success",false);
            result.put("msg","该分词已经存在");
        }else{
            boolean success = wordLibraryService.addWord(word);
            if(success){
                result.put("success",true);
                result.put("msg","添加分词成功");
            }else{
                result.put("success",false);
                result.put("msg","添加分词失败");
            }
        }

        this.sendJSON(response,toJson(result));

    }

    @RequestMapping("download")
    public void download(HttpServletResponse res) throws IOException {
        OutputStream os = res.getOutputStream();
        try {
            res.reset();
            res.setHeader("Content-Disposition", "attachment; filename=dict.txt");
            res.setContentType("application/octet-stream; charset=utf-8");
            os.write(FileUtils.readFileToByteArray(new File(WordLibraryService.filePath)));
            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

    public void sendJSON(HttpServletResponse response, String json) {
        try {
            response.setContentType("application/json");
            System.out.println("json="+json);
            response.getOutputStream().write(json.getBytes("UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public String toJson(Object object) {
        String jsonString = JSON.toJSONString(object);
        System.out.println(jsonString);
        return jsonString;
    }
}
